package com.shoujifeng.snowmusic.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.R.integer;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.geniuseoe2012.lazyloaderdemo.cache.ImageLoader;
import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.R.drawable;
import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.AnimaAccess;
import com.shoujifeng.snowmusic.player.http.ServerAccess;
import com.shoujifeng.snowmusic.player.interfaces.MoreInterface;


public class OnlineListAdapter extends BaseAdapter {

	private static final String TAG = "LoaderAdapter";
	private boolean mBusy = false;
	private boolean bConllectHave = false;
	private boolean bDownloadHave = false;
	private DatabaseUtil mDB;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	private ImageLoader mImageLoader;
	private Context mContext;
	private int lay; 
	private int[] key; 
	private String[] keys; 
	private ArrayList<HashMap<String, Object>> array; // list
	private int background; 
	private String[] dbkeys; 
	private int[] dbids; // 

	//上一次点击的MoreView
	private View mLastMore;
	
	//点击回调
	private MoreInterface moreInterface;

	public OnlineListAdapter(Context context,
			ArrayList<HashMap<String, Object>> array, 
			int[] key, String keys[],String[] dbkeys, int[] dbids, int background,int lay) {
		
		this.mContext = context;
		this.lay = lay;
		this.array = array;
		this.key = key;
		this.keys = keys;
		this.background = background;
		this.dbkeys = dbkeys;
		this.dbids = dbids;
		mImageLoader = new ImageLoader(context);
		mLastMore = null;
		mDB = new DatabaseUtil(context);
	}

	public void addInterfaceOnMore(MoreInterface more)
	{
		moreInterface = more;
	}
	public ImageLoader getImageLoader() {
		return mImageLoader;
	}

	@Override
	public int getCount() {
		return array.size();
	}

	@Override
	public HashMap<String, Object> getItem(int position) {
		return array.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		
		//if (convertView == null ) 
		//{
			convertView = LayoutInflater.from(mContext).inflate(lay, null);
		//}

		HashMap<String, Object> map = getItem(position);

		for (int i = 0; i < keys.length; i++) {
			TextView text = (TextView) convertView.findViewById(key[i]);
			text.setText(map.get(keys[i]).toString());
		}

		for (int i = 0; i < dbkeys.length; i++) {
			
			ImageView iv = (ImageView) convertView.findViewById(dbids[i]);
			iv.setImageResource(background);
			mImageLoader.DisplayImage(map.get(dbkeys[i]).toString(),iv, false,background);
		}
		TextView text = (TextView) convertView.findViewById(R.id.type);
		if(text!= null){
			if(map.get(ServerAccess.LAN)!=null){
				if(map.get(ServerAccess.LAN).equals("3366")){
					text.setText("藏");
				}
				else{
					text.setText("");
				}
				
			}
			
		}
		
		ImageView imgMore = (ImageView) convertView.findViewById(R.id.moreImg);
		
		//设置弹出菜单
		if(imgMore != null)
		{ 
			//得到弹出菜单， 将Pos储存
			View moreView = (View)convertView.findViewById(R.id.music_more_lay);
			moreView.setTag(position);
			
			//将弹出菜单存到按钮里
			imgMore.setTag(moreView);
			imgMore.setOnClickListener(moreOnClickListener);
			
			//隐藏所有弹出菜单
			moreView.setVisibility(View.GONE);
			if(mLastMore != null)
			{	
				mLastMore.setVisibility(View.GONE);
				mLastMore = null;
			}
			
			//添加相关点击
			Button btnDownload = (Button)moreView.findViewById(R.id.onHotList_btn_download);
			Button btnConllect = (Button)moreView.findViewById(R.id.onHotList_btn_conllect);
			btnDownload.setTag(position);
			btnConllect.setTag(position);
			btnDownload.setOnClickListener(downloadOnClickListener);
			btnConllect.setOnClickListener(conllectOnClickListener);
		}
		
		return convertView;
	}

	static class ViewHolder {
		static TextView mTextView;
		static ImageView mImageView;
	}

	//更多按下
	private View.OnClickListener moreOnClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {

			View moreView = (View)v.getTag();
			int index = Integer.parseInt(moreView.getTag().toString());
			
			Button btnDownload = (Button)moreView.findViewById(R.id.onHotList_btn_download);
			Button btnConllect = (Button)moreView.findViewById(R.id.onHotList_btn_conllect);
			
			HashMap<String, Object> map = array.get(index);
			
			int uid = Integer.parseInt(map.get("index").toString());
			bConllectHave = mDB.haveConllect(uid);
			
			if(bConllectHave) {
				btnConllect.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_collect_have));
			}
			else {
				btnConllect.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.onlinelike));
			}
			
			bDownloadHave = mDB.haveDownload(uid);
			if(bDownloadHave) {
				btnDownload.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_download_have));
			}
			else {
				btnDownload.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.onlinedownload));
			}
			//如果上一次点击了菜单， 隐藏之
			if(mLastMore != null) {
				mLastMore.setVisibility(View.GONE);
			}
			
			//滑动动画
			AnimaAccess anim = new AnimaAccess(mContext);
			moreView.startAnimation(anim.alphaAnimation(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 200, false));
			
			moreView.setVisibility(View.VISIBLE);
			
			//储存这次按下的菜单
			mLastMore = moreView;
		}
	};
	
	//下载按下
	private View.OnClickListener downloadOnClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			if(bDownloadHave)
				return;
			
			int position = Integer.parseInt(v.getTag().toString());
			
			moreInterface.OnClickDownload(position);
			
			mLastMore.setVisibility(View.GONE);
		}
	};
	
	//收藏按下
		private View.OnClickListener conllectOnClickListener = new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				int position = Integer.parseInt(v.getTag().toString());
				
				moreInterface.OnClickConllect(position, bConllectHave);
				
				mLastMore.setVisibility(View.GONE);
			}
		};
}
